mod stack;
mod context;
mod command;

use context::Context;
use command::Command;

fn parse_input() -> (Context, Vec<Command>) {
    let input = include_str!("../input.txt");

    let mut iter = input.split("\r\n\r\n");
    let contex_input = iter.next().unwrap();
    let command_input = iter.next().unwrap();

    (
        contex_input.into(), 
        command_input
            .lines()
            .map(|l| l.into())
            .collect()
    )
}

fn part1() {
    let (mut context, commands) = parse_input();
    for command in commands {
        context.execute(command);
    }

    let mut result = String::new();
    for stack in context.stacks() {
        result.push(stack.last());
    }

    println!("Part 1: {}", result);
}

fn part2() {
    let (mut context, commands) = parse_input();
    for command in commands {
        context.execute_retain_order(command);
    }

    let mut result = String::new();
    for stack in context.stacks() {
        result.push(stack.last());
    }

    println!("Part 1: {}", result);
}

fn main() {
    part1();
    part2();
}
